package com.zany.system.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.zany.system.service.LogInfoService;
import com.zany.system.vo.LogInfoVO;
import com.zany.util.ZanyErrorMsg;
import com.zany.util.ZanyException;
import com.zany.util.action.AbstractActionSupport;
import com.zany.util.ajax.AjaxTools;
import com.zany.util.file.ExeclHeader;
import com.zany.util.file.FileUtil;
import com.zany.util.json.JsonTools;

public class LogInfoAction extends AbstractActionSupport{

	private static final long serialVersionUID = -1528175059477153863L;
	private static Log logger = LogFactory.getLog(LogInfoAction.class);
	private LogInfoService logInfoService;
	private LogInfoVO logInfoVO = new LogInfoVO();
	private List<LogInfoVO> logInfoList = new ArrayList<LogInfoVO>();
	
	public void getList() throws ZanyException{
		if(logger.isInfoEnabled()){logger.info("LogInfoAction.getList()");}
		response = ServletActionContext.getResponse();
		Integer pageTotal = logInfoService.getLogInfoCount(logInfoVO);
		String start = request.getParameter("start");
		String pageStart = (start== null || start.equals("")?"1":start);
		logInfoVO.setPageMessage(Integer.valueOf(pageStart));
		logInfoList = logInfoService.getLogInfoList(logInfoVO);
		AjaxTools.exAjax("{results:" + pageTotal + ",ZANY:" + JsonTools.lstBeanToJson(logInfoList) + "}", response);
	}

	public void toExcel() throws ZanyException{
		if(logger.isInfoEnabled()){logger.info("LogInfoAction.toExcel()");}
		response = ServletActionContext.getResponse();
		logInfoList = logInfoService.getLogInfoListAll();
		try{
			if(!logInfoList.isEmpty()){
				FileUtil.listToExcel(request, response, ExeclHeader.EXECL_HEADER_LOGQUERY, logInfoList, ExeclHeader.EXECL_LOGQUERY);
			}else{
				String returnMsg = "无查询结果！";
				AjaxTools.exAjax("{success:false,msg:\""+returnMsg+"\"}",response);
			}
		}catch(Exception e){
			AjaxTools.exAjax("{success:false}", response);
			throw new ZanyException(ZanyErrorMsg.FILE_ERROR,e.getMessage(),e);
		}
	}
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		
	}

	public LogInfoService getLogInfoService() {
		return logInfoService;
	}

	public void setLogInfoService(LogInfoService logInfoService) {
		this.logInfoService = logInfoService;
	}

}
